package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.SupplierService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: SupplierController
 * Description:
 * Date: 2022/1/15 15:54
 *
 * @version:
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {



    @Autowired
    private SupplierService supplierService;



    @PostMapping("/list")
    public Map<String,Object> listPage(Integer page, Integer rows, String supplierName){

        Page<Supplier> supplierList = supplierService.selectPage(page,rows,supplierName);


        HashMap<String, Object> stringObjectHashMap = new HashMap<>();


        stringObjectHashMap.put("total",supplierList.getTotal());

        stringObjectHashMap.put("rows",supplierList.getResult());
        return stringObjectHashMap;
    }




    @PostMapping("/save")
    public ServiceVO insertOrUpdateSupplier(Supplier supplier){





        try {
            supplierService.insertOrUpdateSupplier(supplier);
            return new ServiceVO<>(100,"请求成功");

        }catch (Exception e){
            e.printStackTrace();
        }

        return new ServiceVO<>(101,"添加或修改失败");


    }


    @PostMapping("/delete")
    public ServiceVO deleteSupplier(String ids){


        try {
            supplierService.deleteSupplierByIds(ids);
            return new ServiceVO<>(100,"请求成功");

        }catch (Exception e){
            e.printStackTrace();
        }

        return new ServiceVO<>(101,"删除失败");
    }


    @PostMapping("/getComboboxList")
    public List<Supplier> getComboboxList(String q){



        List<Supplier>supplierList =  supplierService.getComboboxList(q);
        return supplierList;





    }

}
